package org.yanghaokun.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yanghaokun.basic.util.AjaxResult;
import org.yanghaokun.basic.util.LoginContext;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.pet.domain.Pet;
import org.yanghaokun.pet.domain.SearchMasterMsg;
import org.yanghaokun.pet.query.SearchMasterMsgQuery;
import org.yanghaokun.pet.service.ISearchMasterMsgService;
import org.yanghaokun.user.domain.LoginInfo;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping("/detail/{id}")
    public SearchMasterMsg getOne(@PathVariable("id")Long id){
        return searchMasterMsgService.findOne(id);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg,HttpServletRequest request) {
        try {
            //既然要发布寻主信息，我们就需要，发布的用户的id信息等等，
            //里面要获取当前登录用户，可以使用loginContext,但是它要一个Request，所以应该放到Controller
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.publish(msg,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败" + e.getMessage());
        }
    }

    //待处理消息-平台管理员或店铺管理员（员工）
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        //这里需要的login Info是因为需要查询emloyee看看他是个否存在shopid，有九十店铺管理员或员工，没有就是平台管理员
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.pending(query,loginInfo.getId());
    }

    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        //这里需要的login Info是因为需要查询emloyee看看他是个否存在shopid，有九十店铺管理员或员工，没有就是平台管理员
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.processed(query,loginInfo.getId());
    }

    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        //这里需要的login Info是因为需要查询emloyee看看他是个否存在shopid，有九十店铺管理员或员工，没有就是平台管理员
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return searchMasterMsgService.query(query,loginInfo.getId());
    }

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request) {
        //因为前端传过来的参数恰好就是pet类里面的字段所以可以直接使用
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            System.out.println(pet);
            searchMasterMsgService.handle(pet,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("处理失败" + e.getMessage());
        }
    }
}
